package cn.yu.takeoutbackend.timingModule.employ;


import cn.yu.takeoutbackend.Notification.service.NotificationService;

import cn.yu.takeoutbackend.constant.NotificationRule;
import cn.yu.takeoutbackend.employeeManagement.entity.Employee;
import cn.yu.takeoutbackend.employeeManagement.service.EmployeeManagementService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;


@Slf4j
public class EmployAuditJob implements Job {

    @Autowired
    @Qualifier("employeeManagementServiceImpl")
    EmployeeManagementService employeeManagementService;
    @Autowired
    private NotificationService notificationService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        log.info("----------------  EmployAuditJob start ----------------");
        // 扫描待审核的员工
        final List<Employee> employees = employeeManagementService.selectEmployeeListByStatus(0);
        if (employees.size() == 0) {
            log.info("---------------- 无待审核员工 ----------------");
            return;
        }
        log.info("待审核员工数量为：{}", employees.size());
        // 组装参数
        Supplier<List<Employee>> dataSupplier = () -> employees;
        Function<Employee, String> idExtractor = employee -> employee.getId();
        Function<Employee, Map<String, Object>> messageConverter = employee -> {
            return new HashMap<String, Object>() {{
                put("type", "NEW_EMPLOYEE");
                put("id", employee.getId());
                put("name", employee.getName());
                put("timestamp", LocalDateTime.now());
            }};
        };

        // 发送通知
        notificationService.checkAndNotify(NotificationRule.employeeAudit, dataSupplier,idExtractor,messageConverter);
        log.info("----------------  EmployAuditJob end ----------------");
    }
}
